package com.ashes.security.frame;


import com.ashes.oppo.po.SysAshesMenuPO;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengxuhui
 */
public class MenuHelper {

    public static List<SysAshesMenuPO> buildTree(List<SysAshesMenuPO> sysMenuList) {
        List<SysAshesMenuPO> trees = new ArrayList<>();
        for (SysAshesMenuPO sysMenu : sysMenuList) {
            if (sysMenu.getParentId().longValue() == 0) {
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param treeNodes
     * @return
     */
    public static SysAshesMenuPO findChildren(SysAshesMenuPO sysMenu, List<SysAshesMenuPO> treeNodes) {
        sysMenu.setChildren(new ArrayList<>());

        for (SysAshesMenuPO it : treeNodes) {
            if(sysMenu.getMenuId().longValue() == it.getParentId().longValue()) {
                if (sysMenu.getChildren() == null) {
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysMenu;
    }
}
